package br.com.gscorp.bankLion.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component
public class BankLionAuthenticationFailureHandler implements
		AuthenticationFailureHandler {

	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws IOException, ServletException {

		this.updateHeaders(response);

		response.getWriter().print(exception.getMessage());
		response.getWriter().flush();
	}

	private void updateHeaders(HttpServletResponse response) {
		response.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
		response.setStatus(420);
	}

}